<?php

namespace app\web\controller;

use app\common\model\customer\Customer as customerModel;
use think\Db;
use app\common\controller\Frontend;
use app\common\model\supplier\Supplier as supplierModel;

class Supplier extends Frontend
{

    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    protected $layout = '';
    public function index(){
        $keywords = input('keywords');
        $where = [];
        if(!empty($keywords)){
            $where['nickname'] = array('like','%'.$keywords.'%');
        }
        $model = new supplierModel;
        $list = $model->where($where)->limit(0,10)->order('id','DESC')->select();
        $total = $model->where($where)->count();

        $this->assign('keywords',$keywords);
        $this->assign('list',$list);
        $this->assign('total',$total);
        $this->assign('purl','users');
        return $this->view->fetch();
    }
    public function getsupplier(){
        $keywords = input('keywords');
        $page = input('page/d',1);
        $pagesize = input('pagesize/d',10);
        $where = [];
        if(!empty($keywords)){
            $where['title'] = array('like','%'.$keywords.'%');
        }
        $model = new supplierModel;
        $list = $model->limit($page,$pagesize)->select();
        $this->success('','',$list);
    }

    public function add(){
        if($this->request->isAjax()){
            $data = array(
                'mobile' => input('mobile'),
                'contacts' => input('contacts'),
                'nickname' => input('nickname'),
                'tel' => input('tel'),
            );
            foreach ($data as $v){
                if(empty($v)){
                    $this->error('参数不能为空');
                }
            }
            $data['remark'] = input('remark');
            $data['createtime'] = time();
            $res = Db::name('supplier')->insert($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->success('添加失败');
            }
        }
        return $this->view->fetch();
    }
    public function edit(){
        $ids = input('ids/d',0);
        $model = new supplierModel;
        $row = $model->where(array('id'=>$ids))->find();
        if($this->request->isAjax()){
            if(empty($row)){
                $this->error('非法操作');
            }
            $data = array(
                'mobile' => input('mobile'),
                'contacts' => input('contacts'),
                'nickname' => input('nickname'),
                'company' => input('company'),
                'address' => input('address'),
                'remark' => input('remark'),
            );
            foreach ($data as $v){
                if(empty($v)){
                    $this->error('参数不能为空');
                }
            }
            $res = Db::name('customer')->where(array('id'=>$ids))->update($data);
            if($res){
                $this->success('修改成功');
            }else{
                $this->success('修改失败');
            }
        }

        $this->assign('row',$row);
        return $this->view->fetch();
    }
    public function choose(){
        $ids = input('ids');
        $keywords = input('keywords');
        $model = new supplierModel;
        $where = [];
        $where['nickname'] = array('like','%'.$keywords.'%');
        $list = $model->where($where)->order('id','desc')->limit(0,10)->select();
        $this->assign('keywords',$keywords);
        $this->assign('ids',$ids);
        $this->assign('list',$list);
        return $this->view->fetch();
    }

}
